package org.peterf.foxish.camera;

import android.opengl.Matrix;

public class FixedCamera implements Camera {

    private float mat[] = null;

    private float x1;

    private float x2;

    private float y1;

    private float y2;

    /**
     * Camera with a rectangle view region. The parameters will be those of the
     * screen coordinates.
     * 
     * @param x1
     *            The rectangle's left-top x.
     * @param y1
     *            The rectangle's left-top y.
     * @param x2
     *            The rectangle's right-bottom x.
     * @param y2
     *            The rectangle's right-bottom y.
     */
    public FixedCamera(float x1, float y1, float x2, float y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }

    /**
     * 
     * @return Height in world coordinates of the view rectangle.
     */
    public float getHeight() {
        return y1 - y2;
    }

    /**
     * 
     * @return Width in world coordinates of the view rectangle.
     */
    public float getWidth() {
        return x2 - x1;
    }

    /**
     * An orthographic matrix.
     */
    public float[] getWorldToStdMat() {
        if (mat == null) {
            mat = new float[16];
            Matrix.orthoM(mat, 0, x1, x2, y2, y1, -1, 1);
        }
        return mat;
    }

    /**
     * Convert from standard device coordinates to world coordinates specified
     * by this camera.
     */
    public float stdToWorldX(float stdX) {
        return stdX * (x2 - x1);
    }

    /**
     * Convert from standard device coordinates to world coordinates specified
     * by this camera.
     */
    public float stdToWorldY(float stdY) {
        return stdY * (y1 - y2);
    }

}
